package com.yichen.rent.utils;

import com.yichen.rent.response.AreaResonse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Component
public class AreaUtil {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${areaurl}")
    private String areaUrl;

    public  Map<String,String> cityInfo(String name) {
        AreaResonse areaResonse = restTemplate.getForObject(areaUrl +"&keywords="+ name, AreaResonse.class);
        String center = areaResonse.getDistricts().get(0).getCenter();
        String[] split = center.split(",");
        Map<String,String> map = new HashMap<>();
        map.put("lng",split[0]);
        map.put("lat",split[1]);
        return map;
    }
}
